thin.imports('/public/home/css/register.css');

thin.require('/public/home/configure/conf.sms');
thin.require('/public/home/configure/conf.complete');
thin.imports('/public/home/configure/conf.minfo.js');
thin.imports('/public/home/configure/conf.opts.js');
thin.imports('/public/home/configure/conf.upload.js');
thin.imports('/public/modules/upload.js');

thin.use(function() {
	var upload = thin.uploadFiles;

	// 路由
	checkLocation();

	$('#sendsms').on('click', verifyGET);

	$('#btn-checkaccess').on('click', function(e) {
		var act = $(this).data('action');
		if(act == 'register') {
			verifySMSCode(function() {
				$.hideLoading();
				checkLocation('viden');
			});
		} else if(act == 'bind') {
			verifySMSCode(wxbind);
		}
	});

	$('#btn-register').on('click', function(e) {
		wxregister();
	});

	$('#btn-cy').on('click', function(e) {
		data_role = 1;
		checkLocation('pass');
	});

	$('#btn-viden').on('click', function() {
		$.modal({
			title: "您是？",
			text: "",
			buttons: [{
					text: "个人天使",
					onClick: function() {
						data_role = 2;
						checkLocation('pass');
					}
				},
				{
					text: "机构投资人",
					onClick: function() {
						data_role = 3;
						checkLocation('mechanism');
					}
				}
			]
		});
	});

	$('#mvolist').on('click', 'a', function(e) {
		var that = $(this),
			tlt = that.attr('data-title');
		$('#ipt-jgname').val(tlt);
		$('[data-itor=#ipt-jgname]').find('.value-text').html(tlt);
		checkLocation('mechanism');
	});

	$('#btn-addjg').on('click', function(e) {
		$.showLoading();
		__getdata_orgadd = {
			'logo': $('#ipt-jg-logo').val(),
			'title': $('#ipt-jg-title').val(),
			'introduction': $('#ipt-jg-info').val()
		};
		factory.request('_org_add');
	});

	$('[layout-mark=uploadFile]').on('change', function(e) {
		var picture = upload.getPicture(e);
		if(picture) {
			$(this).parent().children('img').attr('src', picture);
			var u = $(this).attr('data-u');
			var fileInput = this.files[0];
			var form = new FormData();
			form.append("file", fileInput);
			form.append("name", "file");
			form.append("dir", 'upload');
			upload.getSrc('/api?common.upload', form, window[u]);
			this.parentNode.reset();
		}
	});

	$('#btn-memberinfo').on('click', function(e) {
		submitMemberInfo();
	});

	$('#btn-memberlike').on('click', function(e) {
		submitMemberLike();
	});

	$('#submit-price').on('click', function(e) {
		var n_n = $('#ipt-ml-begin_money').val(),
			n_p = $('#ipt-ml-end_money').val();
		(n_n != '' && n_p != '') && $('#btn-edit-price').find('.value-text').html(n_n + '-' + n_p + '万元');
		$.closePopup();
	});
});

var __getdata_minfoadd = {},
	__getdata_mlikeadd = {};

function submitMemberInfo() {
	$('[id*=ipt-mi]').each(function(index) {
		var that = $(this),
			name_arr = that.attr('id'),
			name = name_arr.split('-')[2];
		__getdata_minfoadd[name] = that.val();
	});
	if(__getdata_minfoadd['headimgurl'] == '' || __getdata_minfoadd['nickname'] == '') {
		$.showLoading();
		factory.request('_m_wxinfo', function() {
			$.showLoading();
			factory.request('_minfo_add');
		});
	} else {
		$.showLoading();
		factory.request('_minfo_add');
	}
}

function __complete_mwxinfo(res, unkn) {
	$.hideLoading();
	if(res.status == 200) {
		__getdata_minfoadd['headimgurl'] = res.data.headimgurl;
		__getdata_minfoadd['nickname'] = res.data.nickname;
	}
	unkn();
}

function __complete_minfoadd(res) {
	$.hideLoading();
	if(res.status == 201) {
		if(data_role == 2 || data_role == 3) {
			checkLocation('memberlike');
		} else {
			$.toast("信息完善成功", 960);
			setTimeout(function() {
				window.location.href = '/mobile/service/member';
			}, 800);
		}
	} else {
		$.toptip(res.error, 'error');
	}
}

function submitMemberLike() {
	$('[id*=ipt-ml]').each(function(index) {
		var that = $(this),
			name_arr = that.attr('id'),
			name = name_arr.split('-')[2];
		__getdata_mlikeadd[name] = that.val();
	});
	$.showLoading();
	factory.request('_mlike_add');
}

function __complete_mlikeadd(res) {
	$.hideLoading();
	if(res.status == 201) {
		$.toast("信息完善成功", 960);
		setTimeout(function() {
			window.location.href = '/mobile/service/member';
		}, 800);
	} else {
		$.toptip(res.error, 'error');
	}
}

function verifyGET() {
	var that = $(this),
		mobile = $('[name=mobile]').val();
	if(mobile == '') {
		$.toptip('请输入手机号', 'error');
		return;
	}
	__getdata_send = {
		'scene': 'bindMobile',
		'mobile': mobile
	}
	factory.request('_sms_send', that);
}

function verifyCD(obj, time) {
	obj.html(time + 's');
	if(time == 0) {
		obj.html('获取验证码');
		obj.on('click', verifyGET);
		obj.removeClass('weui-btn_disabled');
		return null;
	}
	setTimeout(function() {
		verifyCD(obj, --time);
	}, 1000);
}

var __ulogo = {
	'onload': function(res) {
		if(res.status == 201) {
			$('#ipt-jg-logo').val(res.data.uri);
		} else {
			$.toptip(res.error, 'error');
		}
	},
	'onerror': function(res) {
		$.toptip('上传失败，请检测图片和网络', 'error');
	}
};

var __uheadimgurl = {
	'onload': function(res) {
		if(res.status == 201) {
			$('#ipt-mi-headimgurl').val(res.data.uri);
		} else {
			$.toptip(res.error, 'error');
		}
	},
	'onerror': function(res) {
		$.toptip('上传失败，请检测图片和网络', 'error');
	}
}

function __complete_orgadd(res) {
	$.hideLoading();
	if(res.status == 201) {
		$('#ipt-jgname').val(__getdata_orgadd.title);
		$('[data-itor=#ipt-jgname]').find('.value-text').html(__getdata_orgadd.title);
		checkLocation('mechanism');
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_orgvolist(res) {
	if(res.status == 200) {
		var ele = '';
		for(var i in res.data) {
			var list = res.data[i];
			ele += '<a class="weui-cell" href="javascript:;" data-title="' + list.title + '">';
			ele += '<div class="weui-cell__hd" style="position: relative;margin-right: 10px;">';
			ele += '<img src="' + list.logo + '" class="pro-logo" >';
			ele += '</div>';
			ele += '<div class="weui-cell__bd">';
			ele += '<p>' + list.title + '</p>';
			ele += '</div>';
			ele += '</a>';
		}
		$('#mvolist').html(ele);
		jude_mlist = true;
	} else {
		$.toptip(res.error, 'error');
	}
}

var data_role = 0,
	jude_mlist = false;

var __getdata_send, __getdata_check, __getdata_wxbind, __getdata_wxreg;

function __complete_send(res, unkn) {
	if(res.status == 200) {
		$.toast('验证码已发送', 'text');
		unkn.off('click');
		unkn.addClass('weui-btn_disabled');
		verifyCD(unkn, 60);
		$('#btn-checkaccess').data('action', res.data.action);
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_check(res, unkn) {
	if(res.status == 201) {
		unkn();
	} else {
		$.hideLoading();
		$.toptip(res.error, 'error');
	}
}

function __complete_wxbind(res) {
	$.hideLoading();
	if(res.status == 201) {
		$.toast("操作成功", 960);
		setTimeout(function() {
			window.location.href = '/mobile/service/member';
		}, 800);
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_wxreg(res) {
	$.hideLoading();
	if(res.status == 201) {
		$.toast("注册成功", 960);
		setTimeout(function() {
			checkLocation('memberinfo');
		}, 800);
	} else {
		$.toptip(res.error, 'error');
	}
}

function verifySMSCode(callback) {
	var mobile = $('[name=mobile]').val(),
		code = $('[name=code]').val();
	if(mobile == '') {
		$.toptip('请输入手机号', 'error');
		return;
	} else if(code == '') {
		$.toptip('请输入验证码', 'error');
		return;
	}
	$.showLoading();
	__getdata_check = {
		'mobile': mobile,
		'code': code
	};
	factory.request('_sms_check', callback);
}

function wxbind() {
	var mobile = $('[name=mobile]').val();
	__getdata_wxbind = {
		'mobile': mobile
	};
	factory.request('_reg_wxbind');
}

function wxregister() {
	var data = {
		'mobile': $('[name=mobile]').val(),
		'role': data_role,
		'organization': $('#ipt-jgname').val(),
		'password': $('[name=password]').val(),
		'confpwd': $('[name=confpwd]').val(),
	};
	if(data.password == '') {
		$.toptip('请输入密码', 'error');
		return;
	} else if(data.confpwd == '') {
		$.toptip('请再次输入密码', 'error');
		return;
	}
	$.showLoading();
	__getdata_wxreg = data;
	factory.request('_reg_wxreg');
}

var __getdata_opts = {};

function __complete_opts(res, unkn) {
	if(res.status == 200) {
		$('#btn-ml-' + unkn).on('click', function(e) {
			var e = e || window.event;
			e.stopPropagation();
			e.preventDefault();
			$(this).select('open');
			return false;
		}).select({
			title: "请选择",
			multi: true,
			items: res.data,
			onClose: function(res) {
				$('#btn-ml-' + unkn).find('.value-text').html(res.data.titles);
				$('#ipt-ml-' + unkn).val(res.data.titles);
			}
		});
	} else {
		// nothing
	}
}

var jude_cityPicker = false;

function checkLocationCallback(hash) {
	if(/mechanism/gi.test(hash) || /mlist/gi.test(hash)) {
		if(!jude_mlist) {
			__getdata_orgvolist = {};
			factory.request('_org_volist');
		}
	}

	if(/memberinfo/gi.test(hash)) {
		if(!jude_cityPicker) {
			$('#ipt-mobile').val($('[name=mobile]').val());
			thin.imports('/public/plug/JQueryWeui/city-picker.min.js', function() {
				$("#btn-city").on('click', function(e) {
					var e = e || window.event;
					e.stopPropagation();
					e.preventDefault();
					$("#btn-city").picker('open');
					return false;
				}).cityPicker({
					title: '选择城市',
					showDistrict: false,
					onClose: function(res) {
						var d = res.displayValue;
						$('#ipt-mi-province').val(d[0]);
						$('#ipt-mi-city').val(d[1]);
						$('#btn-city').find('.value-text').html(d[0] + '  ' + d[1]);
					}
				});
				jude_cityPicker = true;
			});
		}
	}

	if(/memberlike/gi.test(hash)) {
		__getdata_opts.name = 'round';
		factory.request('_common_opts', 'round');
		__getdata_opts.name = 'industry';
		factory.request('_common_opts', 'industry');
		__getdata_opts.name = 'city';
		factory.request('_common_opts', 'city');
	}
}